<?php
// +----------------------------------------------------------------------
// | B5Yii2CMF V3.0 [快捷通用基础管理开发平台]
// +----------------------------------------------------------------------
// | Author: 冰舞 <357145480@qq.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace api\modules\admin\controllers;

use api\modules\admin\models\LoginForm;
use api\utils\libs\BaseController;
use common\extend\exception\ErrorCode;
use Gregwar\Captcha\CaptchaBuilder;


class PublicController extends BaseController
{
    /**
     * 生成验证码
     * @return array
     */
    public function actionCaptcha(): array
    {
        $obj = new CaptchaBuilder(4);
        $obj->setBackgroundColor(255, 255, 255);
        $obj->build();
        $code = $obj->getPhrase();
        $uuid = md5(strtolower($code) . $this->app->params['md5_secret_key']);
        return $this->success('', ['img' => $obj->inline(), 'uuid' => $uuid]);
    }

    /**
     * 登录
     * @return array
     */
    public function actionLogin(): array
    {
        $model = new LoginForm();
        if (!$model->load($this->request->post(), '')) return $this->error('未获取到数据');
        $token = $model->login();
        if (!$token) return $this->error($model->error ?: '登录失败');
        return $this->success('登录成功', ['token' => $token]);
    }

    public function actions(): array
    {
        return [
            'upload-file' => [
                'class' => 'common\actions\UploadAction',
                'type' => 'file',
                'success' => ErrorCode::API_SUCCESS,
                'error' => ErrorCode::API_ERROR
            ],
        ];
    }

    /**
     * 初始化配置
     * @return array
     */
    public function actionInit(): array
    {
        $config = [
            'root_role_id' => $this->app->params['root_role_id'],
            'root_dept_id' => $this->app->params['root_dept_id'],
            'root_admin_id' => $this->app->params['root_admin_id']
        ];
        return $this->success('', ['config' => $config]);
    }
}
